#include "func.h"
#include "Configuration.h"
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>


Configuration::Configuration(const string& confPath)
{
    using Json = nlohmann::json;
    //对文件内容进行json解析
    //1.打开json文件
    std::ifstream jsonFile(confPath);
    if(!jsonFile.is_open()){
        std::cerr<<"无法打开JSON文件"<< std::endl;
        exit(-1);
    }
    //2.读取文件内容并解析为Json对象
    Json jsonObj = Json::parse(jsonFile);
    //关闭文件
    jsonFile.close();

    string PathToConf = "../conf/";
    string engPath = jsonObj["engPath"];
    string zhPath = jsonObj["zhPath"];
    string stopWordPath = jsonObj["stopWordPath"];
    string webPagePath = jsonObj["webPagePath"];
    /* std::cout << "engPath = " << engPath << "\n"; */
    /* std::cout << "zhPath = " << zhPath << "\n"; */
    /* std::cout << "engPath = " << engPath << "\n"; */

    //赋值数据成员
    _engPath = PathToConf +engPath;
    _zhPath =PathToConf +  zhPath;
    _stopWordsPath = PathToConf + stopWordPath;
    _webPagePath = PathToConf + webPagePath;
}

int Configuration::buildConf(){
    int ret = buildEngFiles();
    if(ret == -1){
        return -1;
    }
    ret = buildZhFiles();
    if(ret == -1){
        return -1;
    }
    ret = buildStopFiles();
    if(ret == -1){
        return -1;
    }
    ret = buildWebPageFiles();
    if(ret == -1){
        return -1;
    }
    return ret;
}

int Configuration::buildEngFiles(){
    //为vertor预分配一些空间,防止频繁的扩容
    _engFiles.reserve(5);
    //打开engPath的目录流
    DIR* dir;
    struct dirent* entry;
    dir = opendir(_engPath.c_str());
    if(dir == nullptr){
        perror("opendir");
        return -1;
    }
    //读出每个文件名
    while((entry = readdir(dir))!= nullptr){
        //拼接出文件的路径
        if(string(entry->d_name)!=string(".")&&string(entry->d_name)!=string("..")){
            string filepath = _engPath +"/"+ entry->d_name;
            /* std::cerr << "eng filepath = " << filepath << "\n"; */
            //加入文件名的vector
            _engFiles.push_back(filepath);
        }}
    return 0;
}

int Configuration::buildZhFiles(){
    //为vertor预分配一些空间,防止频繁的扩容
    _zhFiles.reserve(5);
    //打开engPath的目录流
    DIR* dir;
    struct dirent* entry;
    dir = opendir(_zhPath.c_str());
    if(dir == nullptr){
        perror("opendir");
        return -1;
    }
    //读出每个文件名
    while((entry = readdir(dir))!= nullptr){
        //拼接出文件的路径
        if(string(entry->d_name)!=string(".")&&string(entry->d_name)!=string("..")){
            string filepath = _zhPath +"/"+ entry->d_name;
            /* std::cerr << "zh filepath = " << filepath << "\n"; */
            //加入文件名的vector
            _zhFiles.push_back(filepath);

        }}
    return 0;
}

int Configuration::buildStopFiles(){
    //为vertor预分配一些空间,防止频繁的扩容
    _stopFiles.reserve(5);
    //打开engPath的目录流
    DIR* dir;
    struct dirent* entry;
    dir = opendir(_stopWordsPath.c_str());
    if(dir == nullptr){
        perror("opendir");
        return -1;
    }
    //读出每个文件名
    while((entry = readdir(dir))!= nullptr){
        //拼接出文件的路径
        if(string(entry->d_name)!=string(".")&&string(entry->d_name)!=string("..")){
            string filepath = _stopWordsPath +"/"+ entry->d_name;
            /* std::cerr << "stop filepath = " << filepath << "\n"; */
            //加入文件名的vector
            _stopFiles.push_back(filepath);

        }}
    return 0;

}

int Configuration::buildWebPageFiles(){
    //为vertor预分配一些空间,防止频繁的扩容
    _webPageFiles.reserve(5);
    //打开engPath的目录流
    DIR* dir;
    struct dirent* entry;
    dir = opendir(_webPagePath.c_str());
    /* std::cerr << "web filepath = " << _webPagePath << "\n"; */
    if(dir == nullptr){
        perror("opendir");
        return -1;
    }
    //读出每个文件名
    while((entry = readdir(dir))!= nullptr){
        //拼接出文件的路径
        if(string(entry->d_name)!=string(".")&&string(entry->d_name)!=string("..")){
            string filepath = _webPagePath +"/"+ entry->d_name;
            /* std::cerr << "web filepath = " << filepath << "\n"; */
            //加入文件名的vector
            _webPageFiles.push_back(filepath);

        }}
    return 0;
}

Configuration::~Configuration(){}
